#include "ImageCanvas.h"
#include "MainFrame.h"

BEGIN_EVENT_TABLE(ImageCanvas, wxWindow)
    EVT_PAINT  (ImageCanvas::OnPaint)
   // EVT_MOTION (ImageCanvas::OnMouseMove)
END_EVENT_TABLE()


ImageCanvas::ImageCanvas(MainFrame *parent)
: wxWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,wxNO_FULL_REPAINT_ON_RESIZE)
{
    m_owner = parent;
//    m_show = Show_Default;
  //  m_smile_bmp = wxBitmap(smile_xpm);
    //m_std_icon = wxArtProvider::GetIcon(wxART_INFORMATION);
    //m_clip = false;
	m_pImage = 0L;

}

void ImageCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
{
	wxPaintDC dc(this);

	if(m_pImage){

		wxRect rect = GetClientRect();

		if(rect.width == 0 || rect.height == 0)
		{
			return;
		}

		// Get the handle of the wxWindow
		HWND hwnd = (HWND)this->GetHandle();

		// Now get the native windows DC for this window
		HDC hdc = ::GetDC(hwnd);

		int w  = m_pImage->GetWidth();
		int h = m_pImage->GetHeight();


		HDC dcbuffer = CreateCompatibleDC(hdc);
		HBITMAP hbuffer = CreateCompatibleBitmap(hdc, w, h);
		SelectObject(dcbuffer, hbuffer); 

		GraphicImageModel::ImageGraph * imgGraph = new GraphicImageModel::ImageGraph();
		model->SetImageGraph()->SetImage(m_pImage);
		GraphicImagePresenter* p = new GraphicImagePresenter(dcbuffer,hbuffer);
		p->SetModel(model);
		p->OnPaint();

		// Now blit the object to the screen
		BitBlt(hdc, 0, 0, rect.width, rect.height, dcbuffer, 0, 0, SRCCOPY);



		DeleteDC(dcbuffer);
		DeleteObject(hbuffer);

		// Because we called ::GetDC make sure we release the handle
		// back to the system or we'll have a memory leak.
		::ReleaseDC(hwnd,hdc);

		delete model;
		delete p;
	}
}
